<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- integratingjs.qdoc -->
  <title>Use Case - Integrating JavaScript in QML | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Use Case - Integrating JavaScript in QML</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#using-javascript-expressions-for-property-values">Using JavaScript Expressions for Property Values</a></li>
<li class="level1"><a href="#adding-javascript-functions-in-qml">Adding JavaScript Functions in QML</a></li>
<li class="level1"><a href="#using-javascript-files">Using JavaScript Files</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Use Case - Integrating JavaScript in QML</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-usecase-integratingjs.html-description -->
<div class="descr"> <a name="details"></a>
<p>JavaScript code can be easily integrated into QML to provide UI logic, imperative control, or other benefits.</p>
<a name="using-javascript-expressions-for-property-values"></a>
<h2 id="using-javascript-expressions-for-property-values">Using JavaScript Expressions for Property Values</h2>
<p>JavaScript expressions can be used in QML as bindings. For example:</p>
<pre class="cpp">

  Item {
      width: Math<span class="operator">.</span>random()
      height: width <span class="operator">&lt;</span> <span class="number">100</span> <span class="operator">?</span> <span class="number">100</span> : (width <span class="operator">+</span> <span class="number">50</span>) <span class="operator">/</span>  <span class="number">2</span>
  }

</pre>
<p>Note that function calls, like Math.random(), will not be revaluated unless their arguments change. So binding to Math.random() will be one random number and not revaluated, but if the width is changed in some other manner, the height binding will be reevaluated to take that into account.</p>
<a name="adding-javascript-functions-in-qml"></a>
<h2 id="adding-javascript-functions-in-qml">Adding JavaScript Functions in QML</h2>
<p>JavaScript functions can be declared on QML items, like in the below example. This allows you to call the method using the item id.</p>
<pre class="qml">

  import QtQuick 2.12

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      <span class="name">id</span>: <span class="name">container</span>
      <span class="name">width</span>: <span class="number">320</span>
      <span class="name">height</span>: <span class="number">480</span>

      <span class="keyword">function</span> <span class="name">randomNumber</span>() {
          <span class="keyword">return</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">360</span>;
      }

      <span class="keyword">function</span> <span class="name">getNumber</span>() {
          <span class="keyword">return</span> <span class="name">container</span>.<span class="name">randomNumber</span>();
      }

      <span class="type"><a href="../qtquick/qml-qtquick-taphandler.html">TapHandler</a></span> {
          <span class="comment">// This line uses the JS function from the item</span>
          <span class="name">onTapped</span>: <span class="name">rectangle</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="name">container</span>.<span class="name">getNumber</span>();
      }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">color</span>: <span class="string">&quot;#272822&quot;</span>
          <span class="name">width</span>: <span class="number">320</span>
          <span class="name">height</span>: <span class="number">480</span>
      }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">id</span>: <span class="name">rectangle</span>
          <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
          <span class="name">width</span>: <span class="number">160</span>
          <span class="name">height</span>: <span class="number">160</span>
          <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
          Behavior on <span class="name">rotation</span> { <span class="type"><a href="../qtquick/qml-qtquick-rotationanimation.html">RotationAnimation</a></span> { <span class="name">direction</span>: <span class="name">RotationAnimation</span>.<span class="name">Clockwise</span> } }
      }

  }

</pre>
<a name="using-javascript-files"></a>
<h2 id="using-javascript-files">Using JavaScript Files</h2>
<p>JavaScript files can be used for abstracting out logic from QML files. To do this, first place your functions inside a .js file like in the example shown.</p>
<pre class="js">

  <span class="comment">// myscript.js</span>
  <span class="keyword">function</span> <span class="name">getRandom</span>(previousValue) {
      <span class="keyword">return</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">previousValue</span> <span class="operator">+</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">90</span>) <span class="operator">%</span> <span class="number">360</span>;
  }

</pre>
<p>Then import the file into any .qml file that needs to use the functions, like the example QML file below.</p>
<pre class="qml">

  import QtQuick 2.12
  import &quot;myscript.js&quot; as Logic

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      <span class="name">width</span>: <span class="number">320</span>
      <span class="name">height</span>: <span class="number">480</span>

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">color</span>: <span class="string">&quot;#272822&quot;</span>
          <span class="name">width</span>: <span class="number">320</span>
          <span class="name">height</span>: <span class="number">480</span>
      }

      <span class="type"><a href="../qtquick/qml-qtquick-taphandler.html">TapHandler</a></span> {
          <span class="comment">// This line uses the JS function from the separate JS file</span>
          <span class="name">onTapped</span>: <span class="name">rectangle</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="name">Logic</span>.<span class="name">getRandom</span>(<span class="name">rectangle</span>.<span class="name">rotation</span>);
      }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">id</span>: <span class="name">rectangle</span>
          <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
          <span class="name">width</span>: <span class="number">160</span>
          <span class="name">height</span>: <span class="number">160</span>
          <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
          Behavior on <span class="name">rotation</span> { <span class="type"><a href="../qtquick/qml-qtquick-rotationanimation.html">RotationAnimation</a></span> { <span class="name">direction</span>: <span class="name">RotationAnimation</span>.<span class="name">Clockwise</span> } }
      }

  }

</pre>
<p class="centerAlign"><img src="images/qml-uses-integratingjs.png" alt="" /></p><p>For further details on the JavaScript engine used by QML, as well as the difference from browser JS, see the full documentation on <a href="../qtqml/qtqml-javascript-expressions.html">JavaScript Expressions in QML Documents</a>.</p>
</div>
<!-- @@@qtquick-usecase-integratingjs.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
